package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    OrderSettingService service;

    @RequestMapping("/upload")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result upload(MultipartFile excelFile){

        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);
            List<OrderSetting> settingList =new ArrayList<>();
            for (String[] string : strings) {

                OrderSetting o = new OrderSetting();
                o.setNumber(Integer.parseInt(string[1]));
                o.setOrderDate(new SimpleDateFormat("yyyy/MM/dd").parse(string[0]));
                settingList.add(o);
            }
            service.addOrderSetting(settingList);
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        }  catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("/findBymonth")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result findBymonth (String month){

        try {
            List<OrderSetting> orderSettingList = service.findBymonth(month);
            List<Map<String,Object>> mapList = new ArrayList<>();

            for (OrderSetting orderSetting : orderSettingList) {
                Map<String,Object> map = new HashMap<>();
                map.put("date",new SimpleDateFormat("dd").format(orderSetting.getOrderDate()));
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                mapList.add(map);
            }
//            System.out.println(mapList);
            return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result edit (@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            orderSettingList.add(orderSetting);
            service.addOrderSetting(orderSettingList);
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ORDERSETTING_FAIL);
        }
    }
}
